﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PizzaSystem
{
    public partial class CreatePlan : Form
    {
        public CreatePlan()
        {
            InitializeComponent();
            getPizza();
        }

        List<Pizza> list;

        SqlDao sqlDao = new SqlDao();

        private void getPizza()
        {
            list = sqlDao.selectPizza();
            if (list.Count > 0)
            {
                price.Text = list[0].P_price.ToString();
                name.Text = list[0].P_name.ToString();
                name.DataSource = list;
                name.DisplayMember = "p_name";
                name.ValueMember = "p_price";
            }
        }
        private void num_TextChanged(object sender, EventArgs e)
        {
            if (num.Text[num.Text.Length - 1] >= '0' && num.Text[num.Text.Length - 1] <= '9')
            {
                if (num.Text == "")
                {
                    t_price.Text = "0.00";
                    return;
                }
                if (long.Parse(num.Text) > 1000000)
                {
                    num.Text = "1000000";
                    MessageBox.Show("一次最多只能生产1000000份");
                    return;
                }
                t_price.Text = (decimal.Parse(price.Text) * int.Parse(num.Text)).ToString("0.00");
            }
            else
            {
                num.Text=num.Text.Remove(num.Text.Length - 1);
            }
        }

        private void submit_Click(object sender, EventArgs e)
        {
            if (place.Text == "")
            {
                MessageBox.Show("请输入一个整数作为生产顺序!");
            }
            else
            {
                Scheme scheme = new Scheme();
                foreach (var item in list)
                {
                    if (item.P_name == name.Text && item.P_price == decimal.Parse(price.Text))
                    {
                        scheme.P_id = item.P_id;
                    }
                }
                scheme.S_num = int.Parse(num.Text);
                scheme.S_id = Utils.getTimeTicks();
                scheme.S_place = int.Parse(place.Text);
                if (sqlDao.insertScheme(scheme))
                {
                    MessageBox.Show("制定计划成功...");
                }
                else
                {
                    MessageBox.Show("制定计划失败...");
                }
            }
        }

        bool f = true;
        private void name_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (f)
            {
                f = false;
                return;
            }
            price.Text = name.SelectedValue.ToString();
            t_price.Text = (decimal.Parse(price.Text) * int.Parse(num.Text)).ToString("0.00");
        }
    }
}
